<?php
class Access_Model_User extends Zend_Db_Table_Abstract
{

    public function deleteRules($roleId = null) {

        if($roleId == null) {
            throw new Exception('Not role id');
        }

        $query = $this->_db
                      ->delete('access_rules',
                               array('roles_id = ?' => $roleId));
    }

    public function insertRules($roleId = null, array $data) {
        
        if(isset($data['administrator'])) {
            $data = array( 'roles_id' => $roleId,
            'resource' => 'all',
            'privileg' => 'all');
            $this->_db->insert('access_rules', $data);
        } else {
            if (is_array($data)) {
            foreach ($data as $app => $controller) {

                foreach ($controller as $controllerName => $action) {

                    $data = array(
                        'roles_id' => $roleId,
                        'resource' => $app . '_' . $controllerName,
                        'privileg' => implode(',', array_keys($action)));
                    $this->_db->insert('access_rules', $data);
                }
            }
            }
        }
    }
}